//
//  MOWorkTaskListViewController.m
//  MobileOffice
//
//  Created by 冰冰 on 2019/8/12.
//  Copyright © 2019 com.idolmedia. All rights reserved.
//

#import "MOWorkTaskListViewController.h"
#import "MOWorkTaskListViewCell.h"
#import "MOWorkTaskSelectdTaskTypeView.h"
#import "MOAddWokTaskViewController.h"
#import "MOWorkTaskDetailViewController.h"

@interface MOWorkTaskListViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UITableView *tableView;

@property(nonatomic,strong)NSMutableArray *dataSource;

@property(nonatomic,strong)UIView *headerView;

@property(nonatomic,strong)UIButton *selectedTaskTypeButton;
//选择的数据数组
@property(nonatomic,strong)NSMutableArray *selectTaskDataAray;
//选择的model
@property(nonatomic,strong)MOWorkTaskSelectdTaskTypeModel *selectedModel;

@end

@implementation MOWorkTaskListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initNav];
    
    [self creatView];
    
    [self initData];
}

-(void)initNav
{
    UIButton *addButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 30, 40)];
    [addButton setImage:IMAGE(@"use_add") forState:UIControlStateNormal];
    [addButton addTarget:self action:@selector(clickAddTask) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc]initWithCustomView:addButton];
    self.navigationItem.rightBarButtonItem = rightButtonItem;
}

-(void)creatView
{
    self.title = @"工作任务";
    
    self.view.backgroundColor = KGrayColor_ededed;
    
    [self.view addSubview:self.headerView];
    
    [self.view addSubview:self.tableView];
}

-(void)initData
{
    self.dataSource = [NSMutableArray array];
    for (int i = 0; i<15; i++) {
        MOWorkTaskListModel *model = [[MOWorkTaskListModel alloc]init];
        model.nameStr = @"熟练掌握数字人事操作系统";
        model.datestr = @"2017-09-09";
        model.taskStatusStr = @"保存";
        model.taskTypeStr = @"推送任务";
        model.auditPeopleStr = @"李开";
        model.auditIdeastr = @"同意";
        [self.dataSource addObject:model];
    }
    [self.tableView reloadData];
    
    self.selectTaskDataAray = [NSMutableArray array];
    for (int i = 0; i<10; i++) {
        MOWorkTaskSelectdTaskTypeModel *model = [[MOWorkTaskSelectdTaskTypeModel alloc]init];
        model.nameStr = [NSString stringWithFormat:@"任务%d",i+1];
        model.idStr = [NSString stringWithFormat:@"%d",i+1];
        [self.selectTaskDataAray addObject:model];
    }
}

#pragma mark -- 选择任务类型
-(void)clickSelectedTaskTypeButton:(UIButton *)sender
{
    sender.selected = !sender.selected;
    MOWorkTaskSelectdTaskTypeView *view = [[MOWorkTaskSelectdTaskTypeView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    __weak typeof (self)weaakSelf = self;
    view.clickSaveButtonBlock = ^(MOWorkTaskSelectdTaskTypeModel * _Nonnull model) {
        weaakSelf.selectedModel = model;
        [weaakSelf.selectedTaskTypeButton setTitle:model.nameStr forState:UIControlStateNormal];
        [weaakSelf.selectedTaskTypeButton SG_imagePositionStyle:(SGImagePositionStyleRight) spacing:10];
    };
    view.clickCancelButtonBlock = ^{
        weaakSelf.selectedTaskTypeButton.selected = NO;
    };
    if (self.selectedModel) {
        view.currentIdStr = self.selectedModel.idStr;
    }
    view.resultArray = self.selectTaskDataAray;
    [WINDOW addSubview:view];
}

#pragma mark -- 添加任务
-(void)clickAddTask
{
    MOAddWokTaskViewController *vc = [[MOAddWokTaskViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark -- UITableViewDelegate,UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataSource.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MOWorkTaskListViewCell *cell = [MOWorkTaskListViewCell cellWithTableView:tableView andIndexPath:indexPath];
    cell.model = self.dataSource[indexPath.row];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50 + 18*6 + 15*5;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section==self.dataSource.count-1) {
        return 13;
    }else{
        return 0.0001;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 13;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MOWorkTaskDetailViewController *vc = [[MOWorkTaskDetailViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}


#pragma mark -懒加载
-(UITableView *)tableView
{
    if (!_tableView) {
        
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(15, self.headerView.bottom, SCREEN_WIDTH - 30, SCREEN_HEIGHT-StatusBarAndNavigationBarHeight - 44) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = KGrayColor_ededed;
        _tableView.showsVerticalScrollIndicator = NO;
        
        //        _tableView.mj_header = [MORefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(downloadAfaterDataSource)];
        //        _tableView.mj_footer = [MORefreshGifFooter footerWithRefreshingTarget:self refreshingAction:@selector(downloadBeforeDataSource)];
        //        [_tableView.mj_header beginRefreshing];
        
    }
    return _tableView;
}

-(UIView *)headerView
{
    if (!_headerView) {
        _headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 44)];
        _headerView.backgroundColor = KWhiteColor;
        
        _selectedTaskTypeButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 44)];
        _selectedTaskTypeButton.titleLabel.font = FONT_SIZE(14);
        [_selectedTaskTypeButton setTitleColor:KBlackColor_222222 forState:UIControlStateNormal];
        [_selectedTaskTypeButton setTitleColor:KBlueColor_2C94FF forState:UIControlStateSelected];
        [_selectedTaskTypeButton setTitle:@"任务类型" forState:UIControlStateNormal];
        [_selectedTaskTypeButton setImage:IMAGE(@"taskDown") forState:UIControlStateNormal];
        [_selectedTaskTypeButton setImage:IMAGE(@"taskTop") forState:UIControlStateSelected];
        [_selectedTaskTypeButton SG_imagePositionStyle:(SGImagePositionStyleRight) spacing:10];
        [_selectedTaskTypeButton addTarget:self action:@selector(clickSelectedTaskTypeButton:) forControlEvents:UIControlEventTouchUpInside];
        [_headerView addSubview:_selectedTaskTypeButton];
        
    }
    return _headerView;
}

@end
